import { cloudFilePdf2txtObtainUrlType, FileMetaType } from '@/types/file';
import request, { RequestConfig } from '@/utils/request';
import { AxiosProgressEvent, AxiosResponse } from 'axios';

export const uploadFile = (
  data: FormData,
  config?: {
    signal?: AbortSignal;
    onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
  }
) =>
  request.post<FileMetaType>('/system/file/upload', data, {
    timeout: 480000,
    ...config,
    headers: {
      'Content-Type': 'multipart/form-data; charset=utf-8'
    }
  });

export const uploadFilePublic = (
  data: FormData,
  config?: {
    signal?: AbortSignal;
    onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
  }
) =>
  request.post<FileMetaType>('/system/file/public/upload', data, {
    timeout: 480000,
    ...config,
    headers: {
      'Content-Type': 'multipart/form-data; charset=utf-8'
    }
  });
export const uploadFileBySharing = (
  data: FormData,
  config?: {
    signal?: AbortSignal;
    onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
  }
) =>
  request.post<FileMetaType>('/client/sharing/app/chat/file/upload', data, {
    timeout: 480000,
    ...config,
    headers: {
      'Content-Type': 'multipart/form-data; charset=utf-8'
    }
  });

export const uploadImageCompressionApi = (
  data: FormData,
  config?: {
    signal?: AbortSignal;
    onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
  }
) =>
  request.post<FileMetaType>('/system/file/public/uploadImageAndCompression', data, {
    timeout: 480000,
    ...config,
    headers: {
      'Content-Type': 'multipart/form-data; charset=utf-8'
    }
  });

export const uploadMultipleFiles = (
  data: FormData,
  config?: {
    signal?: AbortSignal;
    onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
  }
) =>
  request.post<FileMetaType[]>('/system/file/batch/upload', data, {
    timeout: 480000,
    ...config,
    headers: {
      'Content-Type': 'multipart/form-data; charset=utf-8'
    }
  });

export const downloadFile = (
  fileKey: string,
  config?: { signal?: AbortSignal; onUploadProgress?: (progressEvent: AxiosProgressEvent) => void }
) =>
  request.post<AxiosResponse>(
    '/system/file/download',
    { objectKey: fileKey },
    {
      timeout: 480000,
      responseType: 'blob',
      ...config
    }
  );
export const downloadPublicFile = (
  fileKey: string,
  config?: { signal?: AbortSignal; onUploadProgress?: (progressEvent: AxiosProgressEvent) => void }
) =>
  request.post<AxiosResponse>(
    '/system/file/public/download',
    { objectKey: fileKey },
    {
      timeout: 480000,
      responseType: 'blob',
      ...config
    }
  );

export const cloudFilePdf2txtObtainUrl = (fileId: string) =>
  request.post<cloudFilePdf2txtObtainUrlType>('/cloud/file/pdf2txt/obtainUrl', { fileId });

export const downloadFileByUrl = (
  url: string,
  config?: {
    signal?: AbortSignal;
    onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
  } & RequestConfig,
  params?: Record<string, unknown>
) =>
  request.post<AxiosResponse>(url, params, {
    timeout: 480000,
    responseType: 'blob',
    ...config
  });

export const getFileList = (fileKeys: string[]) =>
  request.post<FileMetaType[]>('/system/file/list', { fileKeys });

export const getFileMeta = (fileKey: string) =>
  request.post<FileMetaType>('/system/file/detail', { objectKey: fileKey });
